package test;

import data.NombreCompleto;

import java.util.Comparator;

public class NameComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        NombreCompleto n1 = (NombreCompleto)o1;
        NombreCompleto n2 = (NombreCompleto)o2;
        int i = 0;
        do{
            if(n1.getApellido().toLowerCase().charAt(i) == n2.getApellido().toLowerCase().charAt(i)){
                i++;
            }else{
                if(n1.getApellido().toLowerCase().codePointAt(i) > n2.getApellido().toLowerCase().codePointAt(i)){
                    return 1;
                }else{
                return -1;
                }
            }

        }while(n1.getApellido().length()>i && n2.getApellido().length()>i);
        if(n1.getApellido().length() == n1.getApellido().length()){
             i = 0;
             do{
                 if(n1.getNombre().toLowerCase().charAt(i) == n2.getNombre().toLowerCase().charAt(i)){
                     i++;
                 }else{
                     if(n1.getNombre().toLowerCase().codePointAt(i) > n2.getNombre().toLowerCase().codePointAt(i)){
                         return 1;
                     }else{
                     return -1;
                     }
                 }

             }while(n1.getNombre().length()>i && n2.getNombre().length()>i);

        }else{
            if(n1.getApellido().length() > n2.getApellido().length()){
                return 1;
            }else{
                return -1;
            }
        }
        if(n1.getNombre().length() > n2.getNombre().length()){
            return 1;
        }else{
            return -1;
        }
    }
}